package fm.dongman.modellib.remote;

import android.support.annotation.NonNull;

import org.json.JSONObject;

import java.util.List;

import fm.dongman.animefm.contractlib.BaseDataSource;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.ComicContract;
import fm.dongman.animefm.contractlib.model.ComicDataModel;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;

/**
 * 番剧页面网络数据源加载
 * Created by KUIGE on 2017/10/17.
 */

public class ComicRemoteDataSource implements ComicContract.IComicDataSource {
    private static ComicRemoteDataSource SINGLETON;

    private RemoteOperationHelper mRemoteOperationHelper;

    /**
     * 是否有网络
     */
    private boolean isRemote = true;

    private ComicRemoteDataSource() {
        mRemoteOperationHelper = RemoteOperationHelper.getOperationHelper();
    }

    public static ComicRemoteDataSource getInstance() {
        if (SINGLETON == null) {
            SINGLETON = new ComicRemoteDataSource();
        }
        return SINGLETON;
    }

    @Override
    public void getComicGuessLike(@NonNull BaseDataSource.LoadSourceCallback<? super List<? extends IModel.IComicModel>> callback) {
        if (isRemote) {
            // 猜你喜欢的换一换
            JSONObject object = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.COMIC_REMOTE + "recent_num=0&tucao_num=0&g_num=5");
            if (object != null) {
                ComicDataModel model = mRemoteOperationHelper.downComicList(object);
                callback.onDataLoaded(model.getGuessLikeComics());
            } else callback.onDataLoadFiled();
        }else callback.onDataLoadFiled();
    }

    @Override
    public void getData(@NonNull BaseDataSource.LoadSourceCallback<? super ComicDataModel> callback) {
        if (isRemote) {
            JSONObject object = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.COMIC_REMOTE + "order=hot&recent_num=5&tucao_num=5&g_num=5");
            if (object != null) {
                ComicDataModel model = mRemoteOperationHelper.downComicList(object);
                callback.onDataLoaded(model);
            } else callback.onDataLoadFiled();
        }else callback.onDataLoadFiled();
    }

    @Override
    public void saveData(ComicDataModel dataModel) {
    }

    @Override
    public void refreshData() {
    }

    @Override
    public void offLine() {
        isRemote =!isRemote;
    }
}
